/* -*- C++ -*- */

//=============================================================================
/*
 * @author Feloweria <380374483@qq.com>
 * Copyright (C) Feloweria
 * 
 * $ID 2013-06-15 07:22:08 PM
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @version 1.0
 */
//=============================================================================
#include <string.h>
FELOWRIA_BEGIN

inline 
CPU_Affinity::CPU_Affinity(CPU_TYPE cpu)
{
    size = CPU_ALLOC_SIZE(info.core_num);
    cpu_set = CPU_ALLOC(size);
    AddCPU(cpu);
}

inline
CPU_Affinity::CPU_Affinity(const CPU_Affinity& affinity)
:size(affinity.size),count(affinity.count)
{
    memcpy(cpu_set,affinity.cpu_set,size);

}

inline
CPU_Affinity::~CPU_Affinity()
{
	free(cpu_set);
	cpu_set = 0;
	size = 0;
	count = 0;
}

inline
bool CPU_Affinity::AddCPU(CPU_TYPE cpu)
{
	if(!IsScope(cpu))
		return false;
	
	if(IsExist(cpu))
		return false;
	
	CPU_SET_S(cpu,size,cpu_set);
	return ++count;	
}

inline
bool CPU_Affinity::RemoveCPU(CPU_TYPE cpu)
{
	if(!IsScope(cpu))
		return false;
	
	if(!IsExist(cpu))
		return false;
	
	CPU_CLR_S(cpu,size,cpu_set);
	return --count;	
	
}

inline
bool CPU_Affinity::Clear()
{
	CPU_ZERO_S(size,cpu_set);
	size = 0;
	count = 0;
	return true;
}

inline
CPU_Affinity& CPU_Affinity::operator=(const CPU_Affinity& affinity)
{
    memcpy(cpu_set,affinity.cpu_set,size);
    size = affinity.size;
    count = affinity.count;
    
    return *this;
}

inline
bool CPU_Affinity::operator==(const CPU_Affinity& affinity)
{
	return CPU_EQUAL_S(size,cpu_set,affinity.cpu_set);
}

inline
CPU_Affinity CPU_Affinity::operator&(const CPU_Affinity &affinity) const
{
	CPU_Affinity result(*this);
	CPU_AND_S(size,result.cpu_set,cpu_set,affinity.cpu_set);
	return result;
}

inline
CPU_Affinity& CPU_Affinity::operator&=(const CPU_Affinity &affinity)
{
	CPU_AND_S(size,cpu_set,cpu_set,affinity.cpu_set);
	return *this;
}

inline
CPU_Affinity CPU_Affinity::operator|(const CPU_Affinity &affinity) const
{
	CPU_Affinity result(*this);
	CPU_OR_S(size,result.cpu_set,cpu_set,affinity.cpu_set);
	return result;
}

inline
CPU_Affinity& CPU_Affinity::operator|=(const CPU_Affinity &affinity)
{
	CPU_OR_S(size,cpu_set,cpu_set,affinity.cpu_set);
	return *this;
}

FELOWRIA_END